package com.tanjor.user.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.tanjor.sys.dubbo.impl.interfaces.ISysRoleDubbo;
import com.tanjor.user.entity.SysUserRole;
import com.tanjor.sys.dubbo.dto.SysRoleDTO;
import com.tanjor.user.dubbo.dto.SysUserRoleDTO;
import com.tanjor.user.mapper.SysUserRoleMapper;
import com.tanjor.user.service.interfaces.ISysUserRoleService;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.tanjor.common.datatable.ColumnFilter;
import com.tanjor.common.datatable.PageReq;
import com.tanjor.common.datatable.PageResp;
import com.tanjor.common.exception.BusinessException;
import com.tanjor.common.util.ObjectUtil;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.toolkit.IdWorker;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Liuwy
 * @since 2017-04-08
 */
@Service("SysUserRoleServiceImpl")
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements ISysUserRoleService {
	
	@Resource
	private ISysRoleDubbo sysRoleDubbo;
	
	/**
	 *  List<SysUserRole> to List<SysUserRoleDTO>
	 */
	private  List<SysUserRoleDTO> copyList(List<SysUserRole> list){
		List<SysUserRoleDTO> entityDTOs = new ArrayList<SysUserRoleDTO>();
		if(list!=null){
			for(SysUserRole entity:list){
				SysUserRoleDTO entityDTO = new SysUserRoleDTO();
				ObjectUtil.copy(entity,entityDTO);
				entityDTOs.add(entityDTO);
			}
		}
		return entityDTOs;
	}
	
	/**
	 * 分页入参构造器
	 * @param pageReq
	 * @return
	 */
	private EntityWrapper<SysUserRole> createWrapper(PageReq pageReq){
		EntityWrapper<SysUserRole> wrapper = new EntityWrapper<SysUserRole>();
		if(pageReq.getColumnFilters()!=null){
			for(ColumnFilter columnFilter:pageReq.getColumnFilters()){
				//按需求构造入参
				
			}
		}
		return wrapper;
	}
	
	@Override
	public PageResp findPage(PageReq pageReq) {
		// TODO Auto-generated method stub
		PageResp page = new PageResp();
		//请求标志位
		page.setDraw(pageReq.getDraw());
		//构造条件
		EntityWrapper<SysUserRole> wrapper = createWrapper(pageReq);
		//统计总数
		Integer totalRows = this.baseMapper.selectCount(wrapper);
		//排序	
		if(StringUtils.isNotBlank(pageReq.getOrderBy())){
			wrapper.orderBy(pageReq.getOrderBy(), pageReq.getIsAsc());
		}
		//查询	
		List<SysUserRoleDTO> list = findPage(wrapper, pageReq.getStart(), pageReq.getLength());				
		
		//构造返回
		page.setData(list);
		page.setRecordsFiltered(totalRows);
		page.setRecordsTotal(totalRows);
		return page;
	}
	
	@Override
	public List<SysUserRoleDTO> findPage(Wrapper<SysUserRole> wrapper,int start,int limit) {
		// TODO Auto-generated method stub
		List<SysUserRole> list = baseMapper.selectPage(new RowBounds(start, limit), wrapper);
		return this.copyList(list);
	}
	
	@Override
	public Map<String,Object> insert(SysUserRoleDTO entityDTO) throws BusinessException{
		// TODO Auto-generated method stub	
		Map<String,Object> map = new HashMap<String,Object>();
		entityDTO.setId(""+IdWorker.getId());
		SysUserRole entity = new SysUserRole();
		ObjectUtil.copy(entityDTO,entity);
		this.insert(entity);
		map.put("result", true);
		return map;
	}
	
	@Override
	public Map<String,Object> update(SysUserRoleDTO entityDTO) throws BusinessException{
		Map<String,Object> map = new HashMap<String,Object>();
		// TODO Auto-generated method stub
		SysUserRole entity = new SysUserRole();
		ObjectUtil.copy(entityDTO,entity);
		this.updateById(entity);
		map.put("result", true);
		return map;
	}	
	
	@Override
	public Map<String, Object> delById(Serializable id) throws BusinessException {
		// TODO Auto-generated method stub
		Map<String,Object> map = new HashMap<String,Object>();
		this.deleteById(id);
		map.put("result", true);	
		return map;
	}
	
	@Override
	public SysUserRoleDTO findById(Serializable id){
		// TODO Auto-generated method stub
		SysUserRoleDTO entityDTO = new SysUserRoleDTO();
		SysUserRole entity = this.selectById(id);
		ObjectUtil.copy(entity,entityDTO);
		return entityDTO;
	}

	@Override
	public List<SysUserRoleDTO> findByUserId(String userId) {
		// TODO Auto-generated method stub
		EntityWrapper<SysUserRole> wrapper = new EntityWrapper<SysUserRole>();
		wrapper.and("user_id={0}",userId);
		List<SysUserRole> list = this.baseMapper.selectList(wrapper);
		return this.copyList(list);
	}

	@Override
	public Map<String, Object> saveUserRole(String userId, String userRoleCodes) throws BusinessException {
		// TODO Auto-generated method stub
		Map<String,Object> map = new HashMap<String,Object>();
		if(StringUtils.isBlank(userId)){
			map.put("result", false);
			map.put("msg", "用户编号不能为空");
			return map;
		}
		
		EntityWrapper<SysUserRole> wrapper = new EntityWrapper<SysUserRole>();
		wrapper.and("user_id={0}",userId);
		this.delete(wrapper);
		
		if(StringUtils.isNotBlank(userRoleCodes)){
			String[] roleCodes = userRoleCodes.split(",");
			for(String roleCode:roleCodes){
				SysUserRole entity = new SysUserRole();
				entity.setId(""+IdWorker.getId());
				entity.setUserId(userId);
				entity.setRoleCode(roleCode);
				this.baseMapper.insert(entity);
			}
		}
		map.put("result", true);
	
		return map;
	}

	@Override
	public SysRoleDTO findRoleByUserId(String userId) {
		// TODO Auto-generated method stub
		List<SysUserRoleDTO> list = this.findByUserId(userId);
		if(list!=null&&list.size()>0){
			SysUserRoleDTO sysUserRoleDTO = list.get(0);
			return sysRoleDubbo.findByCode(sysUserRoleDTO.getRoleCode());
		}
		return null;
	}
	
}
